package com.sgb.kjwl.view.ui.activity.material;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMaterialListBinding;
import com.sgb.kjwl.di.material.DaggerMaterialComponent;
import com.sgb.kjwl.di.material.MaterialModule;
import com.sgb.kjwl.model.entity.MaterialFilterEntity;
import com.sgb.kjwl.model.entity.ProvinceEntity;
import com.sgb.kjwl.model.entity.SearchMaterialEntity3;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopGoodsDetailActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.ui.adapter.search.SearchMaterialAdapter3;
import com.sgb.kjwl.view.widget.AreaWindow3;
import com.sgb.kjwl.view.widget.DictWindow4;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.view.widget.MaterialFliterPopWindow;
import com.sgb.kjwl.view.widget.MultiWindow4;
import com.sgb.kjwl.viewmodel.material.MaterialListModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
@Deprecated
public class MaterialListActivity extends AppActivity implements BasePopWindow.OnResultClick {

    @Inject
    MaterialListModel viewModel;
    private ActivityMaterialListBinding binding;
    private String title;
    private String id;
    private String fId;  //用户筛选类别查询
    private int timeSort = 1;
    private int priceSort = 1;
    private int tempPriceSort = 1;
    private int tempTimeSort = 1;
    private SearchMaterialAdapter3 adapter;
    private MaterialFliterPopWindow popWindow;
    private List<DictEntity3> specList;
    private List<MaterialFilterEntity> areaList;
    private AreaWindow3 areaWindow3;
    private List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList;
    private DictWindow5 typeWindow5;
    private String typeId = "";
    private List<DictEntity3> typeDictEntityList;
    private DictWindow5 nameWindow5;
    private String nameId = "";
    private List<DictEntity3> nameDictEntityList;
    private DictWindow5 brandWindow5;
    private String brandId = "";
    private List<DictEntity3> brandDictEntityList;
    private MultiWindow4 storeMultiWindow4;
    private List<DictEntity3> storeDictEntityList;
    private String distanceChecked = "";
    private DictWindow4 supplierDistanceWindow4;
    private List<DictEntity3> distanceDictEntityList;
    private List<String> b = new ArrayList<>();

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMaterialComponent.builder().appComponent(appComponent).materialModule(new MaterialModule(this)).build().inject(this);
    }

    public static void start(Context context, String title, String id, String mId) {
        Intent intent = new Intent();
        intent.putExtra("title", title);
        intent.putExtra("id", id);
        intent.putExtra("mId", mId);
        intent.setClass(context, MaterialListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        // getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_material_list);
        binding.ivBack.setVisibility(View.VISIBLE);
        binding.ivBack.setOnClickListener(this);
        binding.ivCart.setOnClickListener(this);
        binding.priceLayout.setOnClickListener(this);
        binding.timeLayout.setOnClickListener(this);
        binding.filterLayout.setOnClickListener(this);
        binding.cityView.setOnClickListener(this);
        binding.typeLayout.setOnClickListener(this);
        binding.nameLayout.setOnClickListener(this);
        binding.storeLayout.setOnClickListener(this);
        binding.brandLayout.setOnClickListener(this);
        binding.distanceLayout.setOnClickListener(this);
        binding.rightView.setOnClickListener(this);

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp6), getActivity().getResources().getColor(R.color.color_F5F5F5)));

        adapter = new SearchMaterialAdapter3(getActivity(), null);
        adapter.setOnItemClick(new AdapterOnItemClick<SearchMaterialEntity3>() {
            @Override
            public void onItemClick(SearchMaterialEntity3 searchMaterialEntity, int position) {
                ShopGoodsDetailActivity.start(getActivity(), searchMaterialEntity.getId(), "", searchMaterialEntity.getSkuid());
            }
        });
        binding.recyclerView.setAdapter(adapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getMaterialModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getMaterialModel(true);
            }
        });

        popWindow = new MaterialFliterPopWindow(getActivity());
        popWindow.setBgView(binding.grayLayout);
        popWindow.setFilterResultClick(new MaterialFliterPopWindow.FilterResultClick() {
            @Override
            public void result(MaterialFilterJson materialFilterJson, List<DictEntity3> data, SparseBooleanArray checkBooleanArray) {
//                viewModel.materialFilterJson.get().setAreaId(materialFilterJson.getAreaId());
                viewModel.materialFilterJson.get().setBrandId(materialFilterJson.getBrandId());
                viewModel.materialFilterJson.get().setCategoryId(materialFilterJson.getCategoryId());
                viewModel.materialFilterJson.get().setItemId(materialFilterJson.getItemId());
                //      viewModel.materialFilterJson.get().setMaterKey(materialFilterJson.getMaterKey());
                viewModel.materialFilterJson.get().setMaterValue(materialFilterJson.getMaterValue());
                viewModel.getMaterialModel(false);

                typeId = materialFilterJson.getCategoryId();
                typeWindow5.setCheckDefaultById(typeWindow5.getCheckDefultId(materialFilterJson.getCategoryId()));
                nameId = materialFilterJson.getItemId();
                nameWindow5.setCheckDefaultById(nameWindow5.getCheckDefultId(materialFilterJson.getItemId()));
                if (materialFilterJson.getBrandId()!=null&&materialFilterJson.getBrandId().size()>0){
                    brandId = materialFilterJson.getBrandId().get(0);
                    brandWindow5.setCheckDefaultById(brandWindow5.getCheckDefultId(materialFilterJson.getBrandId().get(0)));
                }
                storeMultiWindow4.setAreaCheckDefault(data, checkBooleanArray);
            }

            @Override
            public void queryTypeChild(String id) {
                viewModel.getFilterNameModel(id);
            }

            @Override
            public void queryNameChild(String id) {
                viewModel.getFilterBrandModel(id);
                viewModel.getFilterBrandSpec(id);
            }
        });

        areaWindow3 = new AreaWindow3(MaterialListActivity.this);
        areaWindow3.setOnCallBack(new AreaWindow3.OnCallBack() {
            @Override
            public void select(ProvinceEntity<List<ProvinceEntity>> listProvinceEntity) {
                viewModel.areaCode.set(String.valueOf(listProvinceEntity.getId()));
                binding.cityView.setText(listProvinceEntity.getShortName());
                viewModel.getMaterialModel(false);
            }

            @Override
            public void filter(List<ProvinceEntity<List<ProvinceEntity>>> entityList) {
                fitlerAreaPop(entityList);
            }

            @Override
            public void closeKeybord() {
                closeKeybord();
            }
        });

        binding.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    closeKeybord();
                    viewModel.materialFilterJson.get().setText(binding.editSearch.getText().toString());
                    viewModel.getMaterialModel(false);
                }
                return false;
            }
        });
        initFilterView();
    }

    private void initFilterView() {
        typeWindow5 = new DictWindow5(MaterialListActivity.this);
        typeWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                typeId = dictEntity.getId();
                viewModel.getFilterNameModel(typeId);

                viewModel.materialFilterJson.get().setCategoryId(dictEntity.getId());
                viewModel.materialFilterJson.get().setItemId(null);
                viewModel.materialFilterJson.get().setBrandId(new ArrayList<>());
                viewModel.getMaterialModel(false);

                popWindow.setTypeDefault(typeWindow5.getCheckDefultPosition(dictEntity.getId()), dictEntity.getId());
                popWindow.setNameDefault(-1, null);
                popWindow.setBrandDefault(-1, null);
            }
        });
        typeWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.typeArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
        nameWindow5 = new DictWindow5(MaterialListActivity.this);
        nameWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                nameId = dictEntity.getId();
                viewModel.getFilterBrandModel(nameId);

                viewModel.materialFilterJson.get().setItemId(dictEntity.getId());
                viewModel.materialFilterJson.get().setCategoryId(typeId);
                viewModel.materialFilterJson.get().setBrandId(new ArrayList<>());
                viewModel.getMaterialModel(false);

                popWindow.setNameDefault(nameWindow5.getCheckDefultPosition(dictEntity.getId()), dictEntity.getId());
                popWindow.setBrandDefault(-1, null);
            }
        });
        nameWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.nameArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
        brandWindow5 = new DictWindow5(MaterialListActivity.this);
        brandWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                brandId = dictEntity.getId();
                b.clear();
                b.add(dictEntity.getId());
                viewModel.materialFilterJson.get().setBrandId(b);
                viewModel.materialFilterJson.get().setCategoryId(typeId);
                viewModel.materialFilterJson.get().setItemId(nameId);
                viewModel.getMaterialModel(false);

                popWindow.setBrandDefault(brandWindow5.getCheckDefultPosition(dictEntity.getId()), dictEntity.getId());
            }
        });
        brandWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.brandArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
        storeMultiWindow4 = new MultiWindow4(MaterialListActivity.this);
        storeMultiWindow4.setOnCallBack(new MultiWindow4.OnCallBack() {
            @Override
            public void result(List<String> storeIds, List<DictEntity3> checkDict3List,  SparseBooleanArray checkBooleanArray) {
              //  viewModel.materialFilterJson.get().setAreaId(storeIds);
                viewModel.getMaterialModel(false);
                popWindow.setAreaDefault(checkDict3List, checkBooleanArray);
            }
        });
        storeMultiWindow4.setOnCloseBack(new MultiWindow4.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.storeArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
        supplierDistanceWindow4 = new DictWindow4(MaterialListActivity.this);
        supplierDistanceWindow4.setOnCallBack(new DictWindow4.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                distanceChecked = dictEntity.getName();
                viewModel.materialFilterJson.get().setDistance(dictEntity.getId());
                viewModel.getMaterialModel(false);
            }
        });
        supplierDistanceWindow4.setOnCloseBack(new DictWindow4.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.distanceArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
    }

    @Override
    protected void init() {
        title = getIntent().getStringExtra("title");
        id = getIntent().getStringExtra("id");
        fId = getIntent().getStringExtra("mId");
        binding.tvTitle.setText(title);
        binding.cityView.setText(viewModel.getAreaNameModel());
        viewModel.areaCode.set(viewModel.getAreaCodeModel());
        if (id.equals("0")) {
            viewModel.materialFilterJson.get().setCid1(fId);
        } else {
            viewModel.materialFilterJson.get().setCid3(id);
            viewModel.materialFilterJson.get().setItemId(id);
        }
        viewModel.getMaterialModel(false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_cart:
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    VerificationCodeLoginActivity.start(this);
                } else {
                    viewModel.checkToken("add_shopping_car");
                }
                break;

            case R.id.price_layout:
                timeSort = 0;
                binding.timeImg.setImageResource(R.mipmap.serch_default);
                if (priceSort == 0) {
                    priceSort = tempPriceSort;
                }
                if (priceSort == 1) {
                    priceSort = 2;
                    tempPriceSort = 2;
                    binding.priceImg.setImageResource(R.mipmap.serch_down);
                } else {
                    priceSort = 1;
                    tempPriceSort = 1;
                    binding.priceImg.setImageResource(R.mipmap.serch_up);
                }
                viewModel.materialFilterJson.get().setTimeSort(timeSort);
                viewModel.materialFilterJson.get().setPriceSort(priceSort);
                viewModel.getMaterialModel(false);
                break;
            case R.id.time_layout:
                priceSort = 0;
                binding.priceImg.setImageResource(R.mipmap.serch_default);
                if (timeSort == 0) {
                    timeSort = tempTimeSort;
                }
                if (timeSort == 1) {
                    timeSort = 2;
                    tempTimeSort = 2;
                    binding.timeImg.setImageResource(R.mipmap.serch_down);
                } else {
                    timeSort = 1;
                    tempTimeSort = 1;
                    binding.timeImg.setImageResource(R.mipmap.serch_up);
                }
                viewModel.materialFilterJson.get().setPriceSort(priceSort);
                viewModel.materialFilterJson.get().setTimeSort(timeSort);
                viewModel.getMaterialModel(false);
                break;

            case R.id.filter_layout:
                popWindow.showPopwindow(binding.filterLayout);
                if (storeDictEntityList == null) {
                    viewModel.getFilterStoreModel();
                }
                if (typeDictEntityList == null) {
                    viewModel.getFilterTypeModel(fId);
                }
                break;
            case R.id.city_view:
                areaWindow3.showPopwindow(binding.cityView);
                areaWindow3.setCurrentArea(viewModel.getAreaNameModel());
                if (provinceEntityList == null) {
                    viewModel.getAllArealModel();
                }
                break;
            case R.id.type_layout:
                binding.typeArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                typeWindow5.setCheckDefaultById(typeId);
                typeWindow5.showPopwindow(binding.typeLayout);
                if (typeDictEntityList == null) {
                    viewModel.getFilterTypeModel(fId);
                }
                break;
            case R.id.name_layout:
                binding.nameArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                nameWindow5.setCheckDefaultById(nameId);
                nameWindow5.showPopwindow(binding.nameLayout);
                if (nameDictEntityList == null) {
                    viewModel.getFilterTypeModel(fId);
                }
                break;
            case R.id.store_layout:
                binding.storeArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                storeMultiWindow4.showPopwindow(binding.storeLayout);
                if (storeDictEntityList == null) {
                    viewModel.getFilterStoreModel();
                }
                break;
            case R.id.brand_layout:
                binding.brandArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                brandWindow5.setCheckDefaultById(brandId);
                brandWindow5.showPopwindow(binding.brandLayout);
                if (brandDictEntityList == null) {
                    viewModel.getFilterTypeModel(fId);
                }
                break;
            case R.id.distance_layout:
                binding.distanceArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                supplierDistanceWindow4.setCheckDefault(distanceChecked);
                supplierDistanceWindow4.showPopwindow(binding.distanceLayout);
                if (distanceDictEntityList == null) {
                    viewModel.getFilterDistance();
                }
                break;
            case R.id.right_view:
                closeKeybord();
                viewModel.materialFilterJson.get().setText(binding.editSearch.getText().toString());
                viewModel.getMaterialModel(false);
                break;
        }

    }

    public void showList(List<SearchMaterialEntity3> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }

            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter.setDatas(list);
        }
    }


    public void showFilterBrandSpec(List<DictEntity3> data) {
        if (data == null) {
            return;
        }
        this.specList = data;
        popWindow.setSpecData(data);
    }

    public void showAreaPop(List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList) {
        this.provinceEntityList = provinceEntityList;
        int select = viewModel.getSelectModel(viewModel.getAreaCodeModel(), provinceEntityList);
        areaWindow3.setData(provinceEntityList, select);
    }

    public void fitlerAreaPop(List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList) {
        int select = viewModel.getSelectModel(viewModel.getAreaCodeModel(), provinceEntityList);
        areaWindow3.setFilterData(provinceEntityList, select);
    }


    public void showTypePopWindow(List<DictEntity3> data) {
        this.typeDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        if (typeId.equals("")) {
            typeId = data.get(0).getId();
        }
        typeWindow5.setData(data);
        typeWindow5.setCheckDefaultById(typeId);
        viewModel.getFilterNameModel(typeId);
        popWindow.setTypeData(data);
    }

    public void showNamePopWindow(List<DictEntity3> data) {
        this.nameDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        nameId = data.get(0).getId();
        nameWindow5.setData(data);
        nameWindow5.setCheckDefaultById(nameId);
        viewModel.getFilterBrandModel(nameId);
        viewModel.getFilterBrandSpec(nameId);
        popWindow.setNameData(data);
    }

    public void showBrandPopWindow(List<DictEntity3> data) {
        this.brandDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        brandWindow5.setData(data);
        popWindow.setBrandData(data);
    }

    public void showStorePopWindow(List<DictEntity3> data) {
        this.storeDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        storeMultiWindow4.setData(data);
        popWindow.setAreaData(data);
    }

    public void showDistancePopWindow(List<DictEntity3> data) {
        this.distanceDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        distanceChecked = data.get(0).getName();
        supplierDistanceWindow4.setData(data);
        supplierDistanceWindow4.setCheckDefault(distanceChecked);
    }

    @Override
    public void result(Object key1, Object key2, Object key3) {

    }

    public void checkTokenView() {
        Intent intent = new Intent();
        intent.putExtra("showPage", "FRAGMENT_SHOPPING");
        MainActivity.start(MaterialListActivity.this, intent);
    }

    public void toLogin() {
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, "");
        MToast.showToast(this, "登录失效，请重新登录");

        VerificationCodeLoginActivity.start(this);
    }
}
